﻿using System;
using System.IO;
using System.Linq;
using ITM495.Lab2.Core;
using ITM495.Lab2.Core.Entities;
using ITM495.Lab2.Impl;

namespace ITM495.Lab2
{
    class Program
    {
        private static String _inputFile;
        private static FileType _inputFileType;

        private static String _outputFile;
        private static FileType _outputFileType;

        private static IReader<Tweet> _reader;
        private static IWriter<Tweet> _writer;

        static void Main(string[] args)
        {
            try
            {

                Console.WriteLine("ITM 495 Lab2");

                if (!OptionsAreValid(args))
                {
                    Console.WriteLine("Invalid Command options: {0}", String.Join(" ", args));
                    DisplayHelp();
                }

                Console.WriteLine("Moving Tweets from {0} to {1}", _inputFile, _outputFile);

                var factory = new Lab2Factory();
                _reader = factory.GetReader(_inputFileType, _inputFile);
                _writer = factory.GetWriter(_outputFileType, _outputFile);

                Console.WriteLine("Reading Tweets....");
                var tweetList = _reader.Read();

                Console.WriteLine("We found {0} tweets in {1}.", tweetList.Count(), _inputFile);

                Console.WriteLine("Writing Tweets....");
                _writer.Write(tweetList);

            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;

                Console.WriteLine("We have encounted an unexpected problem. Use the information below to identify the source of our isues.");
                Console.WriteLine("---------------------------------------------");
                Console.WriteLine(ex.ToString());
                Console.WriteLine();

                Console.ResetColor();
            }
            finally
            {
                Console.WriteLine("Program Complete...");
                Console.ReadKey(false);
            }
           
        }

        /// <summary>
        /// Method to validate the arguments passed into the program
        /// </summary>
        private static bool OptionsAreValid(string[] args)
        {
            if(args == null)
                return false;

            if(args.Length != 2)
                return false;

            if(!IsInputFileValid(args[0]))
                return false;

            if(!IsOutputFileValid(args[1]))
                return false;
            
            return true;
        }

        private static bool IsOutputFileValid(string path)
        {
            var extension = Path.GetExtension(path).Remove(0, 1); ;

            try
            {
                _outputFileType = (FileType)Enum.Parse(typeof(FileType), extension, true);
                _outputFile = path;

                return true;
            }
            catch
            {
                return false;
            }
        }

        static void DisplayHelp()
        {
            Console.WriteLine(ResourceHelper.ReadEmbeddedResource("ITM495.Lab2.Help.txt"));
        }

        static bool IsInputFileValid(string path)
        {
            if(!File.Exists(path))
                return false;

            try
            {
                var extension = Path.GetExtension(path).Remove(0, 1);

                _inputFileType = (FileType)Enum.Parse(typeof(FileType), extension, true);
                _inputFile = path;

                return true;
            }
            catch 
            {
                return false;
            }
        }
    }
}
